package com.example.weather;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;

import com.example.weather.fragment.CityFragment;
import com.example.weather.fragment.IndexFragment;
import com.example.weather.fragment.MoreFragment;
import com.example.weather.fragment.RegisterFragment;
import com.google.android.material.navigation.NavigationView;

public class WeatherActivity extends AppCompatActivity {

    private Toolbar toolbar2;
    private DrawerLayout drawerLayout;
    private FrameLayout framelayout;
    private NavigationView navigation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather);
        initView();
        Intent intent=getIntent();
        setSupportActionBar(toolbar2);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        ActionBarDrawerToggle toggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar2,R.string.app_name,R.string.app_name){
            @Override
            public  void  onDrawerOpened(View drawerView){
                super.onDrawerOpened(drawerView);
            }
            @Override
            public  void  onDrawerClosed(View drawerView){
                super.onDrawerClosed(drawerView);
            }
        };
        toggle.syncState();
        drawerLayout.addDrawerListener(toggle);

        getSupportFragmentManager().beginTransaction()
                .add(R.id.framelayout, IndexFragment.newInstance("",""),"")
                .commit();
        toolbar2.setTitle("天气预报APP");
        navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                item.setCheckable(true);
                switch (item.getItemId()){
                    case  R.id.itemIndex:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.framelayout,IndexFragment.newInstance("",""),"")
                                .commit();
                        toolbar2.setTitle("首页");
                        break;
                    case  R.id.itemCity:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.framelayout, CityFragment.newInstance("",""),"")
                                .commit();
                        toolbar2.setTitle("城市管理");
                        break;
                    case  R.id.itemRegister:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.framelayout, RegisterFragment.newInstance("",""),"")
                                .commit();
                        toolbar2.setTitle("登录");
                        break;
                    case R.id.itemMore:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.framelayout, MoreFragment.newInstance("",""),"")
                                .commit();
                        toolbar2.setTitle("更多");
                        break;
                }
                drawerLayout.closeDrawers();
                return  true;
            }
        });


    }


    private void initView() {
        toolbar2 = (Toolbar) findViewById(R.id.toolbar2);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        framelayout = (FrameLayout) findViewById(R.id.framelayout);
        navigation = (NavigationView) findViewById(R.id.navigation);
    }
}